import com.cherry.mybatis.Emp;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.List;

public class T01 {

    @Test
    public void q01() throws IOException {
        //加载核心配置文件
        InputStream resourceAsStream = Resources.getResourceAsStream("SqlMapConfig.xml");
//获得sqlSession工厂对象，从类路径下，文件系统或web Url中加载资源文件
        SqlSessionFactory sqlSessionFactory = new
                SqlSessionFactoryBuilder().build(resourceAsStream);
//获得sqlSession对象，openSession()默认开启一个事务，但事务不会自动提交
        //openSession(boolean autoCommit) true，不需要手动提交事务
        SqlSession sqlSession = sqlSessionFactory.openSession();
//执行sql语句
/*
        <T> T selectOne(String statement, Object parameter)
       <E> List<E> selectList(String statement, Object parameter)
        int insert(String statement, Object parameter)
        int update(String statement, Object parameter)
        int delete(String statement, Object parameter)

        void commit()
void rollback()
        */
        List<Emp> userList = sqlSession.selectList("test01Mapper.findAll");
//打印结果
        System.out.println(userList);
//释放资源
        sqlSession.close();
    }

    @Test
    public void q02() throws IOException {
        InputStream inputStream = Resources.getResourceAsStream("SqlMapConfig.xml");
        SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = sessionFactory.openSession();
        Emp emp1 = sqlSession.selectOne("test01Mapper.findById", 2L);
        System.out.println(emp1);
        sqlSession.close();
    }

    @Test
    public void i01() throws IOException {
        //加载核心配置文件
        InputStream resourceAsStream = Resources.getResourceAsStream("SqlMapConfig.xml");
//获得sqlSession工厂对象
        SqlSessionFactory sqlSessionFactory = new
                SqlSessionFactoryBuilder().build(resourceAsStream);
//获得sqlSession对象
        SqlSession sqlSession = sqlSessionFactory.openSession();
        Emp test01 = new Emp("哈哈哈", "男", 200.0, new Date(), 2);
        int insert = sqlSession.insert("test01Mapper.add", test01);
        System.out.println(insert);
//提交事务
        sqlSession.commit();
        //释放资源
        sqlSession.close();
    }

    @Test
    public void u01() throws IOException {
        //加载核心配置文件
        InputStream resourceAsStream = Resources.getResourceAsStream("SqlMapConfig.xml");
//获得sqlSession工厂对象
        SqlSessionFactory sqlSessionFactory = new
                SqlSessionFactoryBuilder().build(resourceAsStream);
//获得sqlSession对象
        SqlSession sqlSession = sqlSessionFactory.openSession();
        Emp test01 = new Emp();
        test01.setId(6);
        test01.setName("嘻嘻");
        int update = sqlSession.update("test01Mapper.update", test01);
        System.out.println(update);
//提交事务
        sqlSession.commit();
        //释放资源
        sqlSession.close();
    }

    @Test
    public void d01() throws IOException {
        //加载核心配置文件
        InputStream resourceAsStream = Resources.getResourceAsStream("SqlMapConfig.xml");
//获得sqlSession工厂对象
        SqlSessionFactory sqlSessionFactory = new
                SqlSessionFactoryBuilder().build(resourceAsStream);
//获得sqlSession对象
        SqlSession sqlSession = sqlSessionFactory.openSession();
        Emp test01 = new Emp();
        test01.setId(6);
        int delete = sqlSession.delete("test01Mapper.delete", test01);
        System.out.println(delete);
//提交事务
        sqlSession.commit();
        //释放资源
        sqlSession.close();
    }

}
